//
//  GoInViewController.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "GoInViewController.h"
#import "Register.h"
#import "LXC_DataBaseHandle.h"
#import "UserModel.h"

@interface GoInViewController () <UITextFieldDelegate>

@end

@implementation GoInViewController

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.title = @"用户登陆";
    
//   [[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:(UIBarMetricsDefault)];
    
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:(UIBarMetricsDefault)];
    
    
    UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake(40, 50, 100, 30)];
    userName.text = @"用户名:";
    userName.backgroundColor = [UIColor clearColor];
    [self.view addSubview:userName];
    [userName release];
    
    UILabel *passWord = [[UILabel alloc] initWithFrame:CGRectMake(40, 110, 100, 30)];
    passWord.text = @"密码:";
    passWord.backgroundColor = [UIColor clearColor];
    [self.view addSubview:passWord];
    [passWord release];
    
    UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(130, 50, 200, 30)];
    tf1.placeholder = @"请输入用户名";
    tf1.borderStyle = UITextBorderStyleRoundedRect;
    tf1.delegate = self;
    tf1.tag = 3000;
    tf1.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:tf1];
    [tf1 release];
    
    UITextField *tf2 = [[UITextField alloc] initWithFrame:CGRectMake(130, 110, 200, 30)];
    tf2.placeholder = @"请输入密码";
    tf2.borderStyle = UITextBorderStyleRoundedRect;
    tf2.delegate = self;
    tf2.tag = 3001;
    tf2.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:tf2];
    [tf2 release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 170, 100, 50);
    button.backgroundColor = [UIColor greenColor];
    [button setTitle:@"登陆" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(220, 170, 100, 50);
    button1.backgroundColor = [UIColor orangeColor];
    [button1 setTitle:@"注册" forState:(UIControlStateNormal)];
    [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(back:)];
    
}
- (void)back:(UIBarButtonItem *)button {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)buttonAction:(UIButton *)button {
    
    UITextField *tf1 = [self.view viewWithTag:3000];
    UITextField *tf2 = [self.view viewWithTag:3001];
    UserModel *model = [UserModel userModel];
    model.userName = tf1.text;
    model.password = tf2.text;
    
    NSInteger flagGoIn = 0;
    
    if (tf1.text.length > 0 && tf2.text.length > 0) {
    
        // 判断数据库中是否存在该用户
        BOOL flag = [[LXC_DataBaseHandle shareDataBaseHandle] selectModelFromUserTab:model];
        if (!flag) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
            
        }else {
            
            // 登录成功

//            flagGoIn = 1;
//            self.blockGoInState(flagGoIn);

            [[LXC_DataBaseHandle shareDataBaseHandle] updateModelFromUserTab:model loginState:1];

            
            // 登陆成功返回页面
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }

    }else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码不能为空" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];

    }
    
    
}
- (void)button1Action:(UIButton *)button {
    
    Register *reg = [[Register alloc] init];
    
    
    [self.navigationController pushViewController:reg animated:YES];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    UITextField *tf = [self.view viewWithTag:textField.tag + 1];
    [textField resignFirstResponder];
    [tf becomeFirstResponder];
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
